<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>盘点任务编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/logic/checkTask";
var grid;
var billSt='${pd.zd003TaskStatus}' ;
$(function(){
	var column1 = [	{ display: 'id', name: 'id', width: 1,hide:true},
				  	{ display: 'zd001TaskId', name: 'zd001TaskId', hide:true},
				  	{ display: 'zd001TackCode', name: 'zd001TackCode', hide:true},
				  	{ display: 'zd002MaterId', name: 'zd002MaterId', width: 1,hide:true},
                  	{ display: '<font color="gray">物料编码</font>', name: 'zd002MaterCode', width:120},
                  	{ display: '<font color="gray">物料名称</font>', name: 'zd002MaterName', width: 100},
                  	{ display: '<font color="gray">型号</font>', name: 'zd002MaterModel', width: 100},
                  	{ display: '<font color="gray">规格</font>', name: 'zd002MaterSpec', width: 100},
                  	{ display: '<font color="gray">颜色</font>', name: 'zd002MaterColor', width: 80},
                  	{ display: '<font color="gray">材质</font>', name: 'zd002MaterQuality', width: 100},
                  	{ display: '<font color="gray">单位</font>', name: 'zd003Unit', width: 60},
                  	{ display: '<font color="gray">库存数量</font>', name: 'zd006HsQuantity', width: 80},
                  	{ display: '<font color="gray">盘点数量</font>', name: 'zd006HsTaskQuantity', width: 80},
				  	{ display: '<font color="gray">差异数量</font>', name:'zd006HsCyQuantity', width: 80},
                  	{ display: '<font color="gray">原pi编号</font>', name: 'zd011PiCode', width: 120},
                  	{ display: 'picode修改', name:'zd011NewPiCode', width: 80},
				  	{ display: '原客户', name:'zd013OldCustomerName', width: 80},
                  	{ display: '客户修改', name:'zd012CustomerName', width: 80}
                  	];
	var column2 = [	{ display: 'id', name: 'id', width: 1,hide:true},
				  	{ display: 'zd001TaskId', name: 'zd001TaskId', hide:true},
				  	{ display: 'zd001TackCode', name: 'zd001TackCode', hide:true},
				  	{ display: 'zd002MaterId', name: 'zd002MaterId', width: 1,hide:true},
                  	{ display: '<font color="gray">物料编码</font>', name: 'zd002MaterCode', width:120},
                  	{ display: '<font color="gray">物料名称</font>', name: 'zd002MaterName', width: 100},
                  	{ display: '<font color="gray">型号</font>', name: 'zd002MaterModel', width: 100},
                  	{ display: '<font color="gray">规格</font>', name: 'zd002MaterSpec', width: 100},
                  	{ display: '<font color="gray">颜色</font>', name: 'zd002MaterColor', width: 80},
                  	{ display: '<font color="gray">材质</font>', name: 'zd002MaterQuality', width: 100},
                  	{ display: '<font color="gray">单位</font>', name: 'zd003Unit', width: 60},
                  	{ display: '<font color="gray">库存数量</font>', name: 'zd006HsQuantity', width: 80},
                  	{ display: '盘点数量<font color="red">*</font>', name: 'zd006HsTaskQuantity', width: 80, editor:{type : 'float'},render:function(item){
                  		var hsQuantity = item.zd006HsQuantity;//库存数量
				  		var tkQuantity = item.zd006HsTaskQuantity;//盘点数量
				  		if((!isNaN(hsQuantity)&&hsQuantity!=''&&hsQuantity!=null&&hsQuantity!=undefined) && 
				  				(!isNaN(tkQuantity)&&tkQuantity!=''&&tkQuantity!=null&&tkQuantity!=undefined)||tkQuantity==0){
				  			item.zd006HsCyQuantity = (parseFloat(tkQuantity)-parseFloat(hsQuantity)).toFixed(4);
				  		}
				  		return (tkQuantity==null||tkQuantity=='')?'0':(1*tkQuantity).toFixed(4);
                  	}},
				  	{ display: '<font color="gray">差异数量</font>', name:'zd006HsCyQuantity', width: 80},
                  	{ display: '<font color="gray">原pi编号</font>', name: 'zd011PiCode', width: 120},
                  	{ display: 'picode修改', name:'zd011NewPiCode', width: 80, editor:{type : 'text'}},
				  	{ display: '原客户', name:'zd013OldCustomerName', width: 80},
                  	{ display: '客户修改', name:'zd012CustomerName', width: 80, editor:{type : 'text'}}
                  ];
	var column3 = null;
	if('ADD'=='${flag}' || ''==billSt){
		column3 = column2;
	}else{
		if(billSt=='RETURN'||'CHECK'==billSt){
			column3 = column2;
		}else{
			column3 = column1;
		}
	}
	grid = $("#table1").ligerGrid({
		columns: column3,
        enabledEdit: true,
        usePager: true,pageSize:1000,pageSizeOptions:[1000],
        parms:[{ name : 'id' , value : '${pd.id}'}],
	 	url : basic_Url+"/gridItems.do",
        width: "100%", height:'90%', checkbox: true,rownumbers:true,
        toolbar: { items: [
                           <c:if test="${pd.zd003TaskStatus=='RETURN' || 'CHECK'==pd.zd003TaskStatus}">
                           { text: '添加物料', click: ItemWindow, icon: 'add' },
                           </c:if>
                           <c:if test="${ctType=='EC'}">
                           { line: true },
                           </c:if>
                           { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif'},
                           { line: true },
                           { text: '导出', click: exportExcel, img: '${ctx}/images/excel.gif'}
                          ]
                 }
    });
	$("#pageloading").hide();
	
	//按钮可见/不可见
	if(billSt=='' || '${flag}'=='ADD'){
		$("#forApple").hide();
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#check").hide();
	}else if(billSt=='DRAFT'){
		$("#forApple").hide();
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#check").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}else if(billSt=='NOSELECT'){
		$("#forSave").hide();
		$("#forSubmit").hide();
		$("#forApple").hide();
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#check").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}else if(billSt=='CHECK'){
		$("#forSave").hide();
		$("#forSubmit").hide();
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}else if(billSt=='AUDIT'){
		$("#forSave").hide();
		$("#forSubmit").hide();
		$("#forApple").hide();
		$("#forExcel").hide();
		$("#check").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}else if(billSt=='RETURN'){
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#check").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}else if(billSt=='END'||billSt=='TOVOID'){
		$("#forSave").hide();
		$("#forSubmit").hide();
		$("#forApple").hide();
		$("#forEnd").hide();
		$("#forBack").hide();
		$("#forExcel").hide();
		$("#check").hide();
		$("#month").attr("disabled",true);
		$("#everday").attr("disabled",true);
	}

	$("#inputForm").validate({			  
		onsubmit: true,
		 /*执行ajaxsubmit  */ 
	    submitHandler: function(editform) {
	        try{			    		
	            url =  basic_Url+"/save.do";
	            var api = frameElement.api;
	        	var W = api.opener;
	            getReturnMessage(url,"inputForm",W.searchAP);
	        }catch(e){alert(e);}
	    }
	});
});

function ItemWindow(){
/* 	if($("#zd001WhCode").val()==""){
		$.dialog.alert("请选择盘点仓库");
   	 	return false;
	} */
	var taskId = $("#id").val();
	var taskCode = $("#zd002TaskCode").val();
	getDG().dialog({
		title:'选择物料',
		width : 650,
		height : 350,
		/* content: 'url:' + sys_context+'/public/windowalert/itemaff.do?code=' + $("#zd001WhCode").val(), */
		content:'url:' + sys_context+'/public/windowalert/itemmanager.do',
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var getSelectedHub = this.content.getSelected();
			var manager = $("#table1").ligerGetGridManager();
			if (getSelectedHub != null) {
				$(getSelectedHub).each(function(){
	                manager.addRow2({
	                	zd001TaskId : taskId,
	                	zd001TaskCode : taskCode,
	                	zd002MaterId : this.id,
						zd002MaterCode : this.zd001Code,
						zd002MaterName : this.zd002Name,
						zd002MaterSpec : this.zd026Size,//规格
						zd002MaterModel: this.zd004Model,//型号
						zd002MaterQuality: this.zd034Quality,//材质
						zd002MaterColor: this.zd033Color,//颜色
						zd003Unit : this.basicStapleUnitDO.zd002Name,
						zd006HsQuantity :'0.000',
						zd006HsTaskQuantity :0
                	});
				});
				this.content.Close();
			}
			return false;
	    },
	    cancelVal: '关闭',
	    cancel: true
	}).zindex();
}
	
function submitForm(param){
	if( "${ctType}"=="0"&&($("#id").val()==null||$("#id").val()==undefined||$("#id").val()=="")){
		 $.ligerDialog.confirm('新建月度盘点单据后会将所有未完成的日常盘点单据作废，请确认!!!!', function (yes) {
			 if(yes){
				 subform(param);
			 }
		  });
	}else{
		subform(param)
	}
 }	
function subform(param){
	//设置ajax全局同步
	var data = grid.getData();
 	if(data.length==0){
		$.dialog.alert("请选择物料");
   	 	return false;
	}
 	var msg="";
	var ba=true;
 	var arr=new Array();
 	var size=0;
 	for(var i=0 ;i<data.length ; i++){
 		var key=data[i].zd002MaterCode+"_"+data[i].zd002MaterName+"_"+data[i].zd002MaterModel+"_"+data[i].zd002MaterSpec+"_"+data[i].zd002MaterColor+"_"+data[i].zd002MaterQuality+"_"+data[i].zd011NewPiCode+"_"+data[i].zd011PiCode;
 		for(var j = 0 ; j<arr.length ; j++){
 			if(arr[j]==key){
 				if(msg==""){
 					msg=(i+1)+"与"+(j+1)+",code为"+data[i].zd002MaterCode+",PICode为"+data[i].zd011NewPiCode+"_"+data[i].zd011PiCode;
 				}else{
 					msg=msg+","+(i+1)+"与"+(j+1)+",code为"+data[i].zd002MaterCode+",PICode为"+data[i].zd011NewPiCode+"_"+data[i].zd011PiCode;;
 				}
 				ba=false;
 				size++;
 			}
 		} 
 		arr.push(key);
 	}
 	if(!ba){
	 	$.dialog.alert("您的第"+msg+",共计"+size+"条数据一致，请合并!!!");
 	}
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#flagMark").val(param);
		$("#checkdata").val(JSON.stringify(data));
		$("#deleteDate").val(JSON.stringify(grid.getDeleted()));
	    $("#inputForm").submit();
	}
}
function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, 
			function (flag) { 
			if(flag){
				submitForm(param);
				}
		}); 
}

function submitApply(param){
	tableSubmit('是否确认申请结束盘点？',param);
}

function submitEnd(param){
	tableSubmit('是否确认结束盘点任务？',param);
}

function submitRebut(param){
	tableSubmit('是否确认驳回重盘？',param);
}

function close(){
	var api = frameElement.api;
	var W = api.opener;
	CloseDialog();
	W.searchAP();
}
function deleteRow(){ 
	grid.deleteSelectedRow();
}

function exportExcel(){
	var id = $("#id").val();
	var path = basic_Url + "/exportExcel.do?taskid=" + id+"&whName="+$("#zd001WhName").val();
   	window.open(path,"_blank");
}

//盘点仓
function TaskWHWindow() {
	getDG().dialog({
		title : '选择盘点仓',
		width : 650,
		height : 350,
		content : 'url:' + sys_context + '/public/windowalert/roleWH.do',    
		lock : true,
		max : true,
		min : true,
		ok : function() {
			var rows = this.content.getSelected();
			
			if(rows==null){
				$.dialog.alert("请选择数据!");
				return;
			}else{
				if(rows.length>1){
					$.dialog.alert("只能选择一行数据!");
					return;
				}else{
					$(rows).each(function() {
						$("#zd001WhName").val(this.zd003Name);
						$("#zd001WhCode").val(this.zd002Code);
						$("#zd001WhId").val(this.id);
						multipleSearch("#table1",true,"",basic_Url+"/whTaskMater.do?whCode="+this.zd002Code);
					});
				}
			}
			this.content.Close();
			return false;
		},
		cancelVal : '关闭',
		cancel : true
	}).zindex();
}
function modifyTime(){
	if( billSt=="DRAFT"||billSt==""||billSt==undefined||billSt==null||billSt=="RETURN"){
		WdatePicker({dateFmt:'yyyy-MM-dd'});
	}
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${pd.id}"/>
<input type="hidden" id="checkdata" name="checkdata" value=""/>
<input type="hidden" id="deleteDate" name="deleteDate" value=""/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${pd.bizVersion}"/>
<input type="hidden" id="flagMark" name="flagMark" value="" />
<input type="hidden" name="zd009BillType" id="zd009BillType" value="${ctType}"/>
<!-- 用于标识提交按钮 -->
 <div class="pageContent" >
    <div class="panel">
    	<table class="TableBlock" align="center">
    			<tr >
		    		<c:if test="${!empty pd.zd002TaskCode }">
	    				<td>单据编号:</td>
	    				<td >
			        		<input type="text" id="zd002TaskCode" name="zd002TaskCode" readonly="readonly" class="inp_width_98p" value="${pd.zd002TaskCode}" />
			        	</td>
			   		</c:if>
			   		<td>盘点类型：</td>
			   		<td>
			   		<c:if test = "${ctType=='0' }">月度盘点</c:if>
			   		<c:if test = "${ctType=='1' }">日常盘点</c:if>
			   		</td>
		    	</tr>
   		 	<tr>
   		 		<td>盘点仓库:</td>
	 			<td>
                   <input type="hidden" id="zd001WhCode" name="zd001WhCode" value="${pd.zd001WhCode}"/>
					<input type="hidden" id="zd001WhId" name="zd001WhId" value="${pd.zd001WhId}"/>
					<input type="text" id="zd001WhName" class="required" name="zd001WhName" value="${pd.zd001WhName}" readonly="readonly"/>
   		        	<input type="button" value="..." onclick="TaskWHWindow();" />
             	</td>
             	<td>制单人:</td>
             	<td>
             		<input type="text" class="inp_width_98p required" id="zd004Agent" name="zd004Agent" value="${pd.zd004Agent }"/>
             		<c:if test="${pd.zd004Agent==null||pd.zd004Agent=='' }">
             			<script>
             				$("#zd004Agent").val('${user.name}');
             			</script>
             		</c:if>
				</td>
             </tr>
   		     <tr>
   		     	<td nowrap="nowrap" width="20%">盘点日期:</td>
				<td width="30%">
					<input id="zd008StepDate" name="zd008StepDate" readonly="readonly" class="inp_width_98p required" onclick="modifyTime()" value="<fmt:formatDate value="${pd.zd008StepDate}" pattern="yyyy-MM-dd" type="both" />" />
				</td>
   		     	<td>备注:</td>
		        <td colspan="3"><input id="zd007Remark"  type="text" class="inp_width_98p" name="zd007Remark" value="${pd.zd007Remark}" /></td>
		     </tr>
        </table>
        <div id="table1"> </div>        
     </div>
     <div class="formBar">
     	<ul>
     		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSave" onclick="submitForm('DRAFT')">保存</button></div></div></li>
		    <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSubmit" onclick="submitForm('NOSELECT')">提交</button></div></div></li>
		    <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="check" onclick="submitForm('CHECK')">盘点保存</button></div></div></li>
			<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forApple" onclick="submitApply('AUDIT')">申请结束盘点</button></div></div></li>
		    <webres:displaycontroller elementId="LOGIC_WH_CHECK_MISSION_SH" description="结束盘点任务">
				<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forEnd" onclick="submitEnd('END')">结束盘点任务</button></div></div></li>
			</webres:displaycontroller>
			<webres:displaycontroller elementId="LOGIC_WH_CHECK_MISSION_BH" description="盘点任务驳回">
			    <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forBack" onclick="submitRebut('RETURN')">驳回重盘</button></div></div></li>		    		    	   
			</webres:displaycontroller>
			<li><div class="button"><div class="buttonContent"><button type="button" id="forExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
		    <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	    </ul>
      </div>    
    </div>
</form>
</body>
</html>
